package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.nikeProject.entity.SportsCaseReport;
import org.jeecg.modules.nikeProject.entity.SportsProject;
import org.jeecg.modules.nikeProject.entity.SportsProjectFile;
import org.jeecg.modules.nikeProject.entity.vo.OssFileVO;
import org.jeecg.modules.nikeProject.entity.vo.SportsCaseReportVO;
import org.jeecg.modules.nikeProject.mapper.SportsCaseReportMapper;
import org.jeecg.modules.nikeProject.mapper.SportsProjectMapper;
import org.jeecg.modules.nikeProject.service.ISportsCaseReportService;
import org.jeecg.modules.nikeProject.service.ISportsProjectFileService;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.mapper.OssFileMapper;
import org.jeecg.modules.oss.service.impl.OssFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 案例征集-案例申报
 * @Author: jeecg-boot
 * @Date:   2024-12-05
 * @Version: V1.0
 */
@Slf4j
@Service
public class SportsCaseReportServiceImpl extends ServiceImpl<SportsCaseReportMapper, SportsCaseReport> implements ISportsCaseReportService {

    @Autowired
    private SportsCaseReportMapper caseReportMapper;

    @Autowired
    private SportsProjectMapper sportsProjectMapper;

    @Autowired
    private ISportsProjectFileService sportsProjectFileService;
    @Autowired
    private OssFileMapper ossFileMapper;

    @Override
    public Result<?> add(SportsCaseReportVO sportsCaseReportVO, String username) {
        String projectId = sportsCaseReportVO.getProjectId();
        if (null == projectId || "".equals(projectId)) {
            return Result.error("未查找到案例信息，请联系管理员处理！");
        }
        //查询当前案例的状态
        SportsProject sportsProject = sportsProjectMapper.selectById(projectId);
        if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
            return Result.error("案例已提交，不允许编辑!");
        }

        SportsCaseReport caseReport = getSportsCaseReportByProjectId(projectId);
        if (null != caseReport ) {
            caseReport.setUpdateTime(new Date());
            caseReport.setUpdateBy(username);
            caseReport.setIzActive(CommonConstant.COMMON_STATUS_Y);
            caseReportMapper.updateById(caseReport);

            List<SportsProjectFile> fileList = sportsProjectFileService.getFileList(projectId, CommonConstant.OSS_FILE_TYPE_FILE);
            sportsProjectFileService.updateFileList(fileList, username);

            List<SportsProjectFile> videoList = sportsProjectFileService.getFileList(projectId, CommonConstant.OSS_FILE_TYPE_VIDEO);
            sportsProjectFileService.updateFileList(videoList, username);
        }
        //更新案例填写步骤
        sportsProject.setProjectStep(CommonConstant.PROJECT_STEP_CASE_REPORT);
        sportsProject.setUpdateBy(username);
        sportsProject.setUpdateTime(new Date());
        log.info("caseName === {}", sportsCaseReportVO.getSportsCaseReport().getCaseName());
        if (null != sportsCaseReportVO.getSportsCaseReport().getCaseName() || !"".equals(sportsCaseReportVO.getSportsCaseReport().getCaseName())){
            sportsProject.setProjectName(sportsCaseReportVO.getSportsCaseReport().getCaseName());
        }
        if (null != sportsCaseReportVO.getSportsCaseReport().getCaseType() || !"".equals(sportsCaseReportVO.getSportsCaseReport().getCaseType())){
            sportsProject.setCaseType(sportsCaseReportVO.getSportsCaseReport().getCaseType());
        }

        sportsProjectMapper.updateById(sportsProject);


        //将图片写入文件表
        List<OssFileVO> fileOssIds = sportsCaseReportVO.getFileOssIds();
        if (null != fileOssIds && fileOssIds.size() > 0) {
//            fileOssIds.forEach(x -> {
//            });
            sportsProjectFileService.batchOssFile(fileOssIds, projectId, username, CommonConstant.OSS_FILE_TYPE_FILE);
        }

        //将视频写入文件表
        List<OssFileVO> videoOssIds = sportsCaseReportVO.getVideoOssIds();
        if (null != videoOssIds && videoOssIds.size() > 0) {
            videoOssIds.forEach(x -> {
                String ossId = IdWorker.getIdStr();
                sportsProjectFileService.addOssFile(ossId, projectId, username, CommonConstant.OSS_FILE_TYPE_VIDEO, null);

                OssFile ossFile = new OssFile();
                ossFile.setId(ossId);
                String url = x.getUrl();
                ossFile.setUrl(url);
                ossFile.setFileName(x.getFileName());
                String snapshot = url + "?x-oss-process=video/snapshot,t_1,f_jpg,w_760,h_400,m_fast";
                ossFile.setSnapshot(snapshot);
                ossFile.setCreateBy(username);
                ossFileMapper.insert(ossFile);
            });
        }
        //新增案例申报书  角色为一线体育老师或是校长时，需判断案例内容是否为空
        SportsCaseReport sportsCaseReport = sportsCaseReportVO.getSportsCaseReport();
        if (CommonConstant.ROLE_TYPE_TE.equals(sportsProject.getRoleType()) || CommonConstant.ROLE_TYPE_SM.equals(sportsProject.getRoleType())) {
            if (null == sportsCaseReport.getCaseContent() || "".equals(sportsCaseReport.getCaseContent())) {
                return Result.error("案例申报书-案例正文内容不能为空，请填写后再提交！");
            }
           /* String str = "file:///";
            if (sportsCaseReport.getCaseContent().contains(str)){
                return Result.error("案例正文内容中的图片不能复制，需上传后再提交！");
            }*/
        }

        sportsCaseReport.setCreateBy(username);
        sportsCaseReport.setCreateTime(new Date());
        int i = caseReportMapper.insert(sportsCaseReportVO.getSportsCaseReport());
        if (i == 0) {
            return Result.error("添加失败", projectId);
        }
        return Result.OK("添加成功！");
    }

    public SportsCaseReport getSportsCaseReportByProjectId(String projectId) {
        LambdaQueryWrapper<SportsCaseReport> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsCaseReport::getIzActive, "0");
        lambdaQueryWrapper.eq(SportsCaseReport::getProjectId, projectId);
        return caseReportMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public Result<?> updateCaseReport(SportsCaseReportVO sportsCaseReportVO, String username) {
        String projectId = sportsCaseReportVO.getProjectId();
        SportsProject sportsProject = sportsProjectMapper.selectById(projectId);
        if (CommonConstant.PROJECT_STEP_FINISH.equals(sportsProject.getProjectStep())){
            return Result.error("案例已提交不能编辑！");
        }

        SportsCaseReport sportsCaseReport = sportsCaseReportVO.getSportsCaseReport();
        sportsCaseReport.setUpdateBy(username);
        sportsCaseReport.setUpdateTime(new Date());
        SportsCaseReport caseReport = caseReportMapper.selectById(sportsCaseReport.getId());
        if (null != caseReport && !"".equals(sportsCaseReport.getProjectId())){
            caseReportMapper.updateById(sportsCaseReport);
        }

        List<OssFileVO> fileOssIds = sportsCaseReportVO.getFileOssIds();
        if (null != fileOssIds && fileOssIds.size() > 0) {
            fileOssIds.forEach(x -> {
                sportsProjectFileService.updateOssId(x.getOssFileId(), projectId, username, CommonConstant.OSS_FILE_TYPE_FILE);
            });
        }

        List<OssFileVO> videoOssIds = sportsCaseReportVO.getVideoOssIds();
        if (null != videoOssIds && videoOssIds.size() > 0) {
            videoOssIds.forEach(x -> {
                sportsProjectFileService.updateOssId(x.getOssFileId(), projectId, username, CommonConstant.OSS_FILE_TYPE_VIDEO);
            });
        }
        return Result.OK("编辑成功!");
    }

    public static void main(String[] args) {
        String str = "file:///";
        String content = "<span><img width=\"16\" height=\"11\" src=\"file:///C:/Users/ADMINI~1/AppData/Local/Temp/ksohtml7128/wps8.png\"></span><span><img width=\"16\" ";
        System.out.println(content.contains(str));
    }

}
